package com.baiyangtx.lingerweather.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.baiyangtx.lingerweather.R;
import com.baiyangtx.lingerweather.adapt.CityKey;


public class MapChartModel implements WeatherChartModel {


	
	
	private static final String[] WEEKDAYS = {"周日" , "周一" , "周二" , "周三" , "周四" , "周五" , "周六"  } ;
	private static final String  TODAY = "今天" ;
	
	private static final String WEATHER_CLOUDY = "多云" ;
	private static final String WEATHER_OVERCAST = "阴天" ;
	private static final String WEATHER_SUNNY = "晴天" ;
	private static final String WEATHER_WINDY = "有风" ;
	private static final String WEATHER_FOG = "有雾" ;
	private static final String WEATHER_HARD_RAIN = "暴雨" ;
	private static final String WEATHER_HEAVY_RAIN = "大雨" ;
	private static final String WEATHER_HEAVY_SNOW = "大雪" ;
	private static final String WEATHER_LIGHT_RAIN = "小雨" ;
	private static final String WEATHER_LIGHT_SNOW = "小雪" ;
	
	private Context context ;
	private Calendar date ;
	private String[] hightTemperature ;
	private String[] lowTemperature ;
	private String[] noonWeather ;
	private String[] nightWeather ;
	
	
	public MapChartModel(Context context , Map<String, String> city ) throws ParseException {
		this.context = context ;
		
		SimpleDateFormat format = new SimpleDateFormat(CityKey.FORMAT , Locale.ENGLISH) ;
		String syncTime = city.get(CityKey.SYNC_TIME) ;
		Date d = format.parse(syncTime) ;
		date = Calendar.getInstance() ; 
		date.setTime(d) ; 
		initData(city) ;
	}
	
	
	
	
	@Override
	public int getDaysNumber() {
		return 6 ;
	}

	@Override
	public String getDayOfWeek(int dayOfNext) {
		String week = null ;
		if( dayOfNext == 0 ){
			week = TODAY ;
		}else{
			int dayOfWeek = date.get(Calendar.DAY_OF_WEEK )  ;
			dayOfWeek = (dayOfWeek + 7 - Calendar.SUNDAY)%7 ; 
			int index = ( dayOfNext + dayOfWeek  ) %7 ;
			week = WEEKDAYS[index ] ;
		}
		return week ;
	}

	@Override
	public String getNoonWeatherText(int dayOfNext) {
		String key = noonWeather[dayOfNext] ;
		return parserWeatherText(key);
	}

	@Override
	public Drawable getNoonWeatherDrawable(int dayOfNext) {
		String key = noonWeather[dayOfNext] ;
		return parserWeatherDrawable(key);
	}

	@Override
	public Drawable getNightWeatherDrawable(int dayOfNext) {
		String key = nightWeather[dayOfNext] ;
		return parserWeatherDrawable(key);
	}

	@Override
	public String getNightWeatherText(int dayOfNext) {
		String key = nightWeather[dayOfNext] ;
		return parserWeatherText(key);
	}

	
	@Override
	@SuppressLint("DefaultLocale")
	public String getDate(int dayOfNext) {
		Calendar day = (Calendar) date.clone() ;
		day.roll(Calendar.DATE, dayOfNext) ;
		int m = day.get(Calendar.MONTH) + 1 ;
		int d = day.get( Calendar.DATE ) ;
		String da = String.format("%02d.%02d", m , d ) ;
		return da ;
	}

	@Override
	public int getHighestTemperature(int dayOfNext) {
		String t = hightTemperature[dayOfNext] ;
		int temp = Integer.valueOf(t) ;
		return temp ;
	}

	@Override
	public int getLowestTemperature(int dayOfNext) {
		String t = lowTemperature[dayOfNext] ;
		int temperature = Integer.valueOf(t) ;
		return temperature ;
	}

	@Override
	public Context getContext() {
		return context ;
	}

	
	
	
	private void initData(Map<String, String> city){
		hightTemperature = city.get(CityKey.FUTURE_TEMP_HIGHT).split(CityKey.SPIT) ;
		lowTemperature  = city.get(CityKey.FUTURE_TEMP_LOW).split(CityKey.SPIT );
		nightWeather  = city.get(CityKey.FUTURE_WEATHER_NIGHT).split(CityKey.SPIT );
		noonWeather  = city.get(CityKey.FUTURE_WEATHER_NOON).split(CityKey.SPIT );
	}
	
	private String parserWeatherText(String key){
		String weather = null ;

		if( key.equals(CityKey.WEATHER_CLOUDY )){
			weather = WEATHER_CLOUDY ;
		}else if(key.equals(CityKey.WEATHER_OVERCAST )) {
			weather = WEATHER_OVERCAST ;
		}else if(key.equals(CityKey.WEATHER_SUNNY )) {
			weather = WEATHER_SUNNY ;
		}else if(key.equals(CityKey.WEATHER_WINDY )) {
			weather = WEATHER_WINDY ;
		}else if(key.equals(CityKey.WEATHER_FOG )) {
			weather = WEATHER_FOG ;
		}else if(key.equals(CityKey.WEATHER_HARD_RAIN )) {
			weather = WEATHER_HARD_RAIN ;
		}else if(key.equals(CityKey.WEATHER_HEAVY_RAIN )) {
			weather = WEATHER_HEAVY_RAIN ;
		}else if(key.equals(CityKey.WEATHER_HEAVY_SNOW )) {
			weather = WEATHER_HEAVY_SNOW ;
		}else if(key.equals(CityKey.WEATHER_LIGHT_RAIN )) {
			weather = WEATHER_LIGHT_RAIN ;
		}else if(key.equals(CityKey.WEATHER_LIGHT_SNOW )) {
			weather = WEATHER_LIGHT_SNOW ;
		}
		
		return weather ;
	}
	
	
	
	private Drawable parserWeatherDrawable(String key){
		int weather  = 0 ;

		if( key.equals(CityKey.WEATHER_CLOUDY )){
			weather = R.drawable.cloudy ;
		}else if(key.equals(CityKey.WEATHER_OVERCAST )) {
			weather = R.drawable.overcast ;
		}else if(key.equals(CityKey.WEATHER_SUNNY )) {
			weather = R.drawable.sunny ;
		}else if(key.equals(CityKey.WEATHER_WINDY )) {
			weather = R.drawable.windy ;
		}else if(key.equals(CityKey.WEATHER_FOG )) {
			weather = R.drawable.fog ;
		}else if(key.equals(CityKey.WEATHER_HARD_RAIN )) {
			weather = R.drawable.hard_rain ;
		}else if(key.equals(CityKey.WEATHER_HEAVY_RAIN )) {
			weather = R.drawable.heavy_rain ;
		}else if(key.equals(CityKey.WEATHER_HEAVY_SNOW )) {
			weather = R.drawable.heavy_snow ;
		}else if(key.equals(CityKey.WEATHER_LIGHT_RAIN )) {
			weather = R.drawable.light_rain ;
		}else if(key.equals(CityKey.WEATHER_LIGHT_SNOW )) {
			weather = R.drawable.light_snow ;
		}
		
		return context.getResources().getDrawable(weather) ;
	}
	
}
